﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetfractionHub.Network.Protocol.BASE.Handler
{
    public class HandlerRCM : ProtocolMessageActionHandler
    {
        public HandlerRCM()
            : base()
        {
            mSupportedConnectionStates = new List<RemoteMachine.ConnectionState>();
        }

        #region ProtocolMessageActionHandler Members

        public override string ProtocolActionCode
        {
            get { return "RCM"; }
        }

        public override bool HandleProtocolMessage(RemoteMachine.Node node, ProtocolMessage message, IEnumerable<ProtocolFeature> extenders)
        {
            if (node is RemoteMachine.Client)
            {
                if (message.Parameters.Count == 2 && message.MessageType == ProtocolMessage.MessageTypeEnum.Direct)
                {
                    if (!Security.Permission.HasPermission(node.SecurityIdentity, typeof(Security.Permissions.DownloadPermission)))
                    {
                        LocalMachine.SecurityBot.GetInstance().SendEchoMessageFromBot(node, Security.Permission.GetDeniedString(typeof(Security.Permissions.DownloadPermission)));
                        return false;
                    }

                    RemoteMachine.Node targetNode = ConnectionManager.GetNodeBySessionId(message.TargetSessionId);

                    if (targetNode == null)
                        return false;

                    if (!Security.Permission.HasPermission(targetNode.SecurityIdentity, typeof(Security.Permissions.SharePermission)))
                    {
                        LocalMachine.SecurityBot.GetInstance().SendEchoMessageFromBot(node, Security.Permission.GetDeniedString(typeof(Security.Permissions.SharePermission)));
                        return false;
                    }

                    string protocol = message.Parameters[0].ParameterValue;
                    string token = message.Parameters[1].ParameterValue;

                    // need to check if they're TCP-active nodes with the SUpports TCP4 or TCP6 feature
                    if (targetNode != null && !((targetNode.Identity.NodeType & IdentityNodeTypes.Bot) == IdentityNodeTypes.Bot | (targetNode.Identity.NodeType & IdentityNodeTypes.Hub) == IdentityNodeTypes.Hub))
                    {
                        // this is a hack to support jUCy!
                        if (message.Parameters[0].ParameterValue == "ADC/0.10")
                        {
                            message.Parameters[0].ParameterValue = "ADC/1.0";
                        }
                        ConnectionManager.SendPeerReverseConnectRequest(message);
                    }
                    else
                    {
                        // send STA.. 
                        Console.WriteLine("Cannot send RCM to: {0}", targetNode.Identity.Nickname);
                    }

                    // should check this too probably...
                    if (protocol == "ADC/1.0" || protocol == "ADC/0.10")
                    {
                    }
                    else
                    {
                    }
                }
                else
                {
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
